#!/usr/bin/python
###############################################################################
#                                                                             #
#    Copyright 2006,2007 Danil Dotsenko                                       #
#                                                                             #
#    This program is free software; you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation; either version 2 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program; if not, write to the Free Software              #
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA #
#                                                                             #
###############################################################################                                   #
#             Danil Dotsenko, dd@accentsolution.com                           #

#from __future__ import division
import karambino

def debug():
	return False

###########################################################################
# applet settings go here, and are accessible from everywhere as global
class configClass:
	'''
	An all-encoupasing, application-wide configuration storage location.
	This will be shared even between concurrently running themes.
	This is the equivalent of root directory of Windows registry.
	'''
	def __init__(self):
		self.common={
			'name':'Karambino Armatura infrastructure example',
			'version':"v1, March 25, 2007",
			'author':"Danil (Suslik) Dotsenko (dd@accentsolution.com)",
			'cleanStart':False,
			'interval':10000,
			}
		self.lang = None
		self.objects = {}
#
##########################################################

def setupObjects(widget):

	karambino.resizeWidget(widget, 150, 150)

	kdesettings = karambino.kde()
	#print kdesettings.version('sk'), kdesettings.screensize(),kdesettings.version('kde')
	
	icons = ['edit', 'editcopy', 'editclear', 'editpaste']
	a = karambino.imageClass(kdesettings.geticon(icons[0], 22), widget=widget)
	a.b = karambino.imageClass(kdesettings.geticon(icons[1], 22), parent=a)
	a.b.c = karambino.imageClass(kdesettings.geticon(icons[2], 22), parent=a.b)
	a.b.c.d = karambino.imageClass(kdesettings.geticon(icons[3], 22), parent=a.b.c)

	def shift(obj, value):
		obj.move(value, 0, 'w','e')
		
	a.position(0, 20)
	a.mousein.signalset('signoflife', shift, a.b, 10)
	a.mouseout.signalset('signoflife', shift, a.b, 2)

	a.b.position(24, 0)
	a.b.mousein.signalset('signoflife', shift, a.b.c, 10)
	a.b.mouseout.signalset('signoflife', shift, a.b.c, 2)

	a.b.c.position(48, 40)
	a.b.c.mousein.signalset('signoflife', shift, a.b.c.d, 10)
	a.b.c.mouseout.signalset('signoflife', shift, a.b.c.d, 2)

	a.b.c.d.position(90, 20)

##################################################################
# Testing & helper functions.
#
def notificationWindow(message, title="", mode=None):
	'''
	This uses KDE kdialog app and asks for main body message and window title.
	App name is attached automatically
	'''
	global appConfig
	myCommand = 'kdialog --caption "'+appConfig.common['name']+'" --title "'+title+'" --msgbox "'+message+'"'
	karambino.execute(myCommand)

def using_dcop():
	#import re
	#from commands import getoutput
	a = "(?<=SuperKaramba:) *\d\.\d"
	ret = getoutput('superkaramba --version')
	try:
		skver = float(re.search(a, ret).group().strip())
	except:
		skver = 100
	if skver < 0.5:
		return True
	else:
		return False

def status(widget, text):
	try:
		meter = karambino.getThemeText(widget, "message")
		karambino.changeText(widget, meter, text)
		karambino.redrawWidget(widget)
	except:
		print "Status Report: ", text

def cleanUpStartUp(widget, success):
	if success:
		# removing the "welcome, loading" images.
		meter = karambino.getThemeImage(widget, "loading")
		karambino.deleteImage(widget,meter)
		meter = karambino.getThemeImage(widget, "status")
		karambino.deleteImage(widget,meter)
		meter = karambino.getThemeText(widget, "message")
		karambino.deleteText(widget,meter)
	else:
		print "FAILURE, NOT SUCCESS!"
		# This is the place that gives you RED SAD / UNHAPPY FACE
		meter = karambino.getThemeImage(widget, "status")
		karambino.setImagePath(widget, meter, 'error.png')
		karambino.changeInterval(widget, 1000000)
#
##################################################################

##################################################################
# KARAMBA's built-in functions
#
#this is called when you widget is initialized
def initWidget(widget):
	global appConfig
	
	status(widget, "Initing the script")
	appConfig=configClass()
	success = True
	
	cleanUpStartUp(widget, success)
	
	if success:
		appConfig.common['cleanStart'] = True
		# createMenus(widget)
		setupObjects(widget)

	karambino.redrawWidget(widget)

def widgetUpdated(widget):
	global appConfig
	if appConfig.common['cleanStart']:
		karambino.changeInterval(widget, appConfig.common['interval'])

def widgetClosed(widget):
	global appConfig
	try:
		if appConfig.common['cleanStart']:
			# put working stuff here
			pass
	except:
		pass

def itemDropped(widget, dropText, x=0, y=0):
	print "in the itemDrag dropText is ", dropText, type(dropText)

#  botton = button clicked:
#                    1 = Left Mouse Button
#                    2 = Middle Mouse Button
#                    3 = Right Mouse Button
#                    4,5 = Scroll wheel up and down
def widgetClicked(widget, x, y, button):
	#print "widgetClicked function, button is ", button
	pass

# NOTE you must use attachClickArea() to make a meterClicked work
def meterClicked(widget, meter, button):
	karambino.meterClicked(widget, meter, button)

def keyPressed(widget, meter, char):
	karambino.keyPressed(widget, meter, char)

def themeNotify(widget, caller, message):
	pass
	
def widgetMouseMoved(widget, x, y, button):
	#Warning:  Don't do anything too intensive here
	karambino.widgetMouseMoved(widget, x, y, button)

# This will be printed when the widget loads.
print "Karambino example is loaded."
